package com.yesep.utils;

import com.yesep.model.ChannelModel;
import io.netty.channel.Channel;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: envm
 * @description: 缓存工具类
 * @author:quliang
 * @create: 2019-07-17 11:06
 **/

public class CacheUtils {
    /**
     * 存放MN与message的对应关系  分包情况下报文存储
     */
    public static final Map<String, String> cacheMessageMap = new ConcurrentHashMap<String, String>(2000);

    /**
     * ip 与  管道的关系
     */
    public static final Map<String, ChannelModel> messageInfoMap = new ConcurrentHashMap<String, ChannelModel>(2000);

    /**
     * 通过ip删除报文缓存
     *
     * @param channel
     */
    public static void removeCacheMessage(Channel channel) {
        cacheMessageMap.remove(channel.remoteAddress().toString());
    }

    /**
     * 添加信息
     *
     * @param mn
     * @param info
     */
    public static void putInfo(String mn, ChannelModel info) {
        if (!Objects.equals(mn, null)) {
            return;
        }
        if (!Objects.equals(info, null)) {
            return;
        }
        messageInfoMap.put(mn, info);
    }

    public static Map getInfos() {
        return messageInfoMap;
    }


}
//    private String cacheName = "DEFAULT_CACHE";
//    private CacheManager cacheManager;
//
//    public CacheUtils(CacheManager cacheManager) {
//        this.cacheManager = cacheManager;
//    }
//
//    public CacheUtils(CacheManager cacheManager, String cacheName) {
//        this.cacheManager = cacheManager;
//        this.cacheName = cacheName;
//    }
//
//    public Object get(String key) {
//        return this.get(this.cacheName, key);
//    }
//
//    public void put(String key, Object value) {
//        this.put(this.cacheName, key, value);
//    }
//
//    public void remove(String key) {
//        this.remove(this.cacheName, key);
//    }
//
//    public Object get(String cacheName, String key) {
//        Element element = this.getCache(cacheName).get(key);
//        return element == null ? null : element.getObjectValue();
//    }
//
//    public void put(String cacheName, String key, Object value) {
//        Element element = new Element(key, value);
//        this.getCache(cacheName).put(element);
//    }
//
//    public void remove(String cacheName, String key) {
//        this.getCache(cacheName).remove(key);
//    }
//
//    public void clean(String cacheName) {
//        this.getCache(cacheName).removeAll();
//    }
//
//    private Cache getCache(String cacheName) {
//        Cache cache = this.cacheManager.getCache(cacheName);
//        if (cache == null) {
//            this.cacheManager.addCache(cacheName);
//            cache = this.cacheManager.getCache(cacheName);
//            cache.getCacheConfiguration().setEternal(true);
//        }
//
//        return cache;
//    }
//
//    public CacheManager getCacheManager() {
//        return this.cacheManager;
//    }
//}
